import { BaseTimer } from "./BaseTimer";
import { connMgr } from "../com/ConnMgr";
import { EventId, eventMgr } from "../com/EventMgr";

/**
 * 模块基类
 */
export abstract class BaseMod extends BaseTimer {

    /** 是否已经初始化 */
    isInited = false;

    declare protected onMsg: typeof connMgr.on;
    declare protected sendMsg: typeof connMgr.send;
    declare protected onEvent: typeof eventMgr.on;
    declare protected onceEvent: typeof eventMgr.once;
    declare protected sendEvent: typeof eventMgr.send;
    declare protected offEvent: typeof eventMgr.off;

    static init() {
        this.prototype.onMsg = function (id: any, cb: any, caller = this) { connMgr.on(id, cb, caller); }
        this.prototype.sendMsg = connMgr.send.bind(connMgr);
        this.prototype.onEvent = function (id: any, cb: any, caller = this) { eventMgr.on(id, cb, caller); }
        this.prototype.onceEvent = function (id: any, cb: any, caller = this) { eventMgr.once(id, cb, caller); }
        this.prototype.sendEvent = eventMgr.send.bind(eventMgr);
        this.prototype.offEvent = function (id: any, cb: any, caller = this) { eventMgr.off(id, cb, caller); }
    }

    /**
     * 模块初始化
     * 由ModMgr调用
     */
    init() {
        this.isInited = true;
        this.onEvent(EventId.reloadXlsx, data => this.onXlsxReload(data));
        this.onInit();
    }

    /**
     * 模块初始化
     * 在这里进行协议和事件的监听
     */
    abstract onInit(): void;

    /**
     * 表格数据重载
     * (虚方法,无需调用super)
     */
    onXlsxReload(reloadDic: { [name: string]: any }): void { }
}